import {
    reqHotSearch,
    reqSearchSuggest,
    reqDefaultSearch,
    reqSearchResult
} from "../../api/search.";
const state = {
    hotSearch: [],
    searchSuggest: [],
    defaultSearch: [],
    searchResult:[],
};

const actions = {
    //获取热搜列表
    async getHotSearch({
        commit
    }) {
        let res = await reqHotSearch();
        if (res.code = 200) {
            commit('GETHOTSEARCH', res.data.result.hots);
        }
    },
    //获取默认搜索关键字
    async getDefaultSearch({ commit }) {
        let res = await reqDefaultSearch();
        if (res.code == 200) {
            commit('GETDEFAULTSEARCH',res.data);
        }
    },
    //获取搜索建议
    async getSearchSuggest({
        commit,state
    }, keyword) {
        if (keyword == '') {
            state.searchSuggest=[]
        }
        else {
            let res = await reqSearchSuggest(keyword, 'mobile');
            if (res.data.code == 200) {
                commit('GETSEARCHSUGGEST', res.data.result.allMatch);
            }
        }
    },
    //获取搜索栏的结果
    async searchResult({ commit }, keywords) {
        let res = await reqSearchResult(keywords);
        if (res.code == 200) {
            commit('SEARCHRESULT',res.data);
        }
        console.log(res.data);
    },
};

const mutations = {
    GETHOTSEARCH(state, data) {
        state.hotSearch = data;
    },
    GETDEFAULTSEARCH(state, data) { 
        state.defaultSearch = data;
    },
    GETSEARCHSUGGEST(state, data) {
        state.searchSuggest = data; //获取建议
        console.log(data)
    },
    SEARCHRESULT(state, data) {
        state.searchResult = data;
    }
};

export default {
    state,
    actions,
    mutations
}